package com.qf.item1.interceptor;

import com.qf.item1.entity.Customer;
import com.qf.item1.utils.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author 刘欣
 * @createTime 2023/2/2 13:21
 * @description 登入验证拦截器
 */
@Slf4j
public class MyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.debug("请求进入拦截器url={}", request.getRequestURI());
        HttpSession session = request.getSession();
        Customer customer = (Customer) session.getAttribute(Constant.CUSTOMER_SESSION_KEY);
        if (customer != null) {
            log.debug("请求被放行url={}", request.getRequestURI());
            return true;
        }
        log.debug("请求被拦截，重定向到登入页");
        response.sendRedirect("/view/login.html");
        return false;
    }
}
